'use strict';
const BaseController = require('../base');
const { searchValidate, detailHouseValidate } = require('@validators/houses');

class HousesController extends BaseController {
  async hots() {
    const { ctx } = this;
    const result = await ctx.service.houses.getHots();
    this.success(result);
  }

  async search() {
    const { ctx } = this;
    await searchValidate(ctx);
    const result = await ctx.service.houses.searchHouses(ctx.params());
    this.success(result);
  }

  async detail() {
    const { ctx } = this;
    const v = await detailHouseValidate(ctx);
    const result = await ctx.service.houses.houseDetail(v.get('query.id'));
    const { imgs, startTime, endTime, publishTime, ...rest } = result.dataValues;
    this.success({
      info: {
        ...rest,
        startTime: ctx.helper.timestamp(startTime),
        endTime: ctx.helper.timestamp(endTime),
        publishTime: ctx.helper.timestamp(publishTime),
      },
      banner: imgs,
    });
  }
}

module.exports = HousesController;
